package graphicinterface;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.LinkedList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import bookworm.Book;

public class WindowBestBooksFromTag 
{
	private LinkedList<Book> bestBooksFromTag;
	private LinkedList<JPanel> panelsWithBooks;
	private JFrame windowBestBooks;
	private String tag;
	
	public WindowBestBooksFromTag(String tag)
	{
		SystemFunctionalitiesFacade knowsSystemFunctionalities = 
				SystemFunctionalitiesFacade.getInstance();
		this.bestBooksFromTag = knowsSystemFunctionalities.getBestBooksFromSpecificTag(tag);
		this.tag = tag;
		this.windowBestBooks = new JFrameWithMenu("best books from tag " + tag);
		this.panelsWithBooks = createPanelsFromBestBooksFromTag();
		drawPageWithBooks();
	}
	
	/**
	 * creates JPanels with book cover, character and rating(on stars) for all books
	 * written by author(no specific tag).
	 * @return
	 */
	private LinkedList<JPanel> createPanelsFromBestBooksFromTag()
	{
		LinkedList<JPanel> panelsFromAllBooks = new LinkedList<JPanel>();
		for(int i = 0; i < bestBooksFromTag.size(); i++)
		{
			Book oneBook = bestBooksFromTag.get(i);
			JPanelWithBookStarsAndCharacter panelOfOneBook =
					new JPanelWithBookStarsAndCharacter(oneBook);
			panelsFromAllBooks.add(panelOfOneBook);
		}
		return panelsFromAllBooks;
	}
	
	private void drawPageWithBooks()
	{
		JPanel panelbestBooksFromTag = new JPanelWithBackgroundImage("backgroundbookworm.jpg");
		GridBagLayout layout = new GridBagLayout();
		panelbestBooksFromTag.setLayout(layout);
		
		JLabel labelWithWindowTitle = 
				TextLabelCreator.createStyledTextlabel("books from tag " + tag, 25);
		JPanel panelWithWindowTitle = new JPanel();
		panelWithWindowTitle.setBackground(new Color(255,131,137));
		panelWithWindowTitle.add(labelWithWindowTitle);
		GridBagConstraints grid = new GridBagConstraints();
		grid.gridheight = 1;
		panelbestBooksFromTag.add(panelWithWindowTitle, grid);
		
		ShowsJPanelsOnManyPages showsPanelsWithBooks =
				new ShowsJPanelsOnManyPages(panelsWithBooks, 4, 1);
		grid.gridy = 1;
		grid.gridheight = 2;
		panelbestBooksFromTag.add(showsPanelsWithBooks, grid);
		
		this.windowBestBooks.add(panelbestBooksFromTag);
		this.windowBestBooks.setVisible(true);
		
	}
}
